//
//  MyWorksListViewController.m
//  AndCampus
//
//  Created by 史宵宵 on 2018/3/9.
//  Copyright © 2018年 史宵宵. All rights reserved.
//

#import "MyWorksListViewController.h"
#import "WorksDetailViewController.h"
#import "WorksDetailInfo.h"

#define KCOLLECTIONCELLWIDTH    ((kScreenW - 5*4)/3)
static NSString * const reuseIdentifier = @"Cell";
@interface MyWorksListViewController ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic,strong) NSMutableArray *dataArray;
@property (nonatomic,weak) UICollectionView *collectionView;
@end

@implementation MyWorksListViewController
-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"我的作品";
    [self creatDataList];
    //1.初始化layout
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    //设置collectionView滚动方向
    //    [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    //设置headerView的尺寸大小
    //    layout.headerReferenceSize = CGSizeMake(SCREEN_WIDTH, 40);
    //该方法也可以设置itemSize
    //    layout.itemSize =CGSizeMake(kScreenW/4.0, 44);
    
    //2.初始化collectionView
    UICollectionView *mainCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH) collectionViewLayout:layout];
    [self.view addSubview:mainCollectionView];
    self.collectionView=mainCollectionView;
    mainCollectionView.backgroundColor = [UIColor clearColor];
    
    [mainCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    mainCollectionView.delegate = self;
    mainCollectionView.dataSource = self;
    
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    
}
#pragma mark - 我的作品
- (void)creatDataList{
    YJNetWorkManger *yjNetWorkManger =  [YJNetWorkManger shareManager];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    [dic setValue:[Global getUserTokenInfo] forKey:@"token"]; //用户的token
    [dic setValue:self.did forKey:@"id"];    //底图的id
    [yjNetWorkManger requestType:GET andURL:[JSPathUtil urlAppInterestGetManZuoPinList] andParmas:dic andComplition:^(id response, BOOL isuccess) {
        if (isuccess) {
            NSArray *array = [NSArray arrayWithArray:response];
            [self.dataArray addObjectsFromArray:[WorksDetailInfo mj_objectArrayWithKeyValuesArray:array]];
            [self.collectionView reloadData];
        }
    }];
}
#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    UIImageView *imageView = [[UIImageView alloc]init];
    imageView.frame = CGRectMake(0, 0, KCOLLECTIONCELLWIDTH, KCOLLECTIONCELLWIDTH);
    WorksDetailInfo *worksDetailInfo = self.dataArray[indexPath.row];
    [imageView sd_setImageWithURL:[NSURL URLWithString:worksDetailInfo.img] placeholderImage:KRECOMMENDLISTDEFAULT];
    imageView.clipsToBounds = YES;
    imageView.layer.borderColor = KBORDERCOLOR.CGColor;
    imageView.layer.borderWidth = 0.5;
    [cell.contentView addSubview:imageView];
    // Configure the cell
    
    return cell;
}

#pragma mark <UICollectionViewDelegate>
//设置每个item的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake( KCOLLECTIONCELLWIDTH, KCOLLECTIONCELLWIDTH);
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    
    return CGSizeMake(kScreenW, 5);
    
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    
    return CGSizeMake(kScreenW, 5);
    
}
//设置每个item水平间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 5;
}


//设置每个item垂直间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 5;
}
//点击item方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    WorksDetailViewController *worksDetailVC = [[WorksDetailViewController alloc]init];
    worksDetailVC.worksDetailInfo = self.dataArray[indexPath.row];
    [self.navigationController pushViewController:worksDetailVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
